// ==================================================================
// 描 述：
// 作 者：王征:D
// 创建时间：2019/06/18 18:00:26
// ==============================================================
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yunchang;

namespace AutoChess
{
    public class HeroShowState : Yunchang.FsmStateBase<HeroBehaviourState>
    {
        private Action endCall;
        private float animPlayTime = 0f;
        private float _elapseTime = 0;
        private bool needPlayEnterAnim = false;

        private HeroBehaviourCtr ctr;

        public HeroShowState(HeroBehaviourState stateType, HeroBehaviourCtr controller) : base(stateType, controller)
        {
            ctr = controller;
        }

        public override void Enter()
        {
            _elapseTime = 0;
            endCall = ctr.showData.callEnd;
            //animPlayTime = ctr.heroRenderer.GetAnimationTime(ctr.showData.animName);
            //ctr.heroRenderer.PlayAnimation(ctr.showData.animName);
            needPlayEnterAnim = true;
        }

        public override void Update()
        {
            if(needPlayEnterAnim)
            {
                if (ctr.heroRenderer == null || !ctr.heroRenderer.IsCanPlayAnim())
                    return;
                needPlayEnterAnim = false;
                animPlayTime = ctr.heroRenderer.GetAnimationTime(ctr.showData.animName);
                ctr.heroRenderer.PlayAnimation(ctr.showData.animName);
            }
            else
            {
                if (_elapseTime >= animPlayTime)
                {
                    if (endCall != null)
                    {
                        endCall();
                        endCall = null;
                    }
                    ctr.SwitchState(HeroBehaviourState.Idle);
                    return;
                }
                _elapseTime += Time.deltaTime;
            }
        }

        public override void Leave()
        {
            endCall = null;
        }
    }
}
